// 循环链表


(function () {
  let ElementNode = null;

  const _headNode = Symbol('_headNode')
  class LoopList {
    constructor() {
      Object.defineProperties(this, {
        [_headNode]: {
          value: null,
          writable: true,
          enumerable: false
        }
      })
    }
    // 插入节点的方法
    insert(element, index = 0) {
      if (this.len() === 0) {
        const node = new ElementNode({ prev: null, next: null, element });
        this[_headNode] = node;
        return node;
      }
      // 找到要插入的位置的上节点和下节点
      if (index < 0 || index > this.len()) {
        throw new Error('index error');
      }
      let findNode = this.find(index)
      let nextNode = index === this.len() ? this[_headNode] : findNode;
      let prevNode = this.len() === 1 ? nextNode : nextNode.getPrev();
      const node = new ElementNode({ prev: prevNode, next: nextNode, element })
      nextNode.setPrev(node)
      prevNode.setNext(node);
      if (index === 0) {
        this[_headNode] = node;
      }
      return node;
    }
    // 删除节点的方法
    remove(index) {
      if (index < 0 || index >= this.len()) {
        throw new Error('index error')
      }
      if (this.len() === 1) {
        this[_headNode] = null;
        return true;
      }
      let findNode = this.find(index);
      let prevNode = findNode.getPrev();
      let nextNode = findNode.getNext();
      findNode.setPrev(null)
      findNode.setNext(null)
      prevNode.setNext(nextNode)
      nextNode.setPrev(prevNode);
      if (findNode === this[_headNode]) {
        this[_headNode] = nextNode;
      }
      return true;
    }
    // 获取链表的长度
    len() {
      if (!this[_headNode]) {
        return 0;
      }
      let sum = 1;
      let n = this[_headNode].getNext();
      while (n && n !== this[_headNode]) {
        n = n.getNext();
        sum += 1;
      }
      return sum;
    }
    // 根据索引取查找节点
    find(index) {
      if (!this[_headNode]) {
        return null;
      }
      if (index < 0 || index >= this.len()) {
        return null;
      }
      if (index === 0) {
        return this[_headNode];
      }
      let n = this[_headNode].getNext();
      let sum = 1;
      while (n && n !== this[_headNode] && index !== sum) {
        n = n.getNext();
        sum += 1;
      }
      if (index === sum) {
        return n;
      } else {
        return null;
      }
    }
    // 查看所有的元素
    toArray() {

      if (!this[_headNode]) {
        return [];
      }
      let arr = [this[_headNode]];
      let n = this[_headNode].getNext();
      while (n && n !== this[_headNode]) {
        arr.push(n);
        n = n.getNext();
      }
      return arr;
    }
    // 查看headNode
    getHead() {
      return this[_headNode];
    }
    // 更新headNode
    setHead(headNode) {
      this[_headNode] = headNode;
    }
  }
  if (typeof module !== 'undefined' && typeof module.exports !== 'undefined' && typeof exports !== 'undefined') {
    ElementNode = require('./elementNode');
    module.exports = LoopList;
  } else if (typeof window) {
    ElementNode = window.ElementNode;
    window.LoopList = LoopList;
  }
})()